//
//  PersonalCenterViewController.m
//  减肥,美容老中医偏方
//
//  Created by appying_wxj on 14/12/12.
//  Copyright (c) 2014年 陈朕. All rights reserved.
//

#import "PersonalCenterViewController.h"
#import "CollectViewController.h"
@interface PersonalCenterViewController ()

@end

@implementation PersonalCenterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        //        self.title = @"妇科偏方";
        UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, boundswidth-120, 44)];
        navLabel.text = @"个人中心";
        navLabel.textColor = [UIColor colorWithRed:1 green:0.94 blue:0.82 alpha:1];
        navLabel.font = [UIFont systemFontOfSize:18];
        navLabel.textAlignment = NSTextAlignmentCenter;
        self.navigationItem.titleView = navLabel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.93 green:0.14 blue:0.28 alpha:0.8];
    [self personalCenterInitUI];
//    [self getFilePath];
}
// 搭建个人中心的界面
-(void)personalCenterInitUI{
    NSArray *nameArr = [[NSArray alloc] initWithObjects:@"我的收藏",@"清除缓存",@"意见反馈",@"版本更新", nil];
    for (int i = 0 ; i < 4 ; i ++) {
        UIButton *mainButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [mainButton setTitle:[nameArr objectAtIndex:i] forState:UIControlStateNormal];
        mainButton.backgroundColor = [UIColor colorWithRed:0.5 green:0.93 blue:0.47 alpha:1];
        mainButton.frame = CGRectMake(20, 80+(60+20)*i, self.view.frame.size.width-40, 60);
        mainButton.tag = i + 111;
        [mainButton addTarget:self action:@selector(mainButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:mainButton];
    }
    
    _cacheLab = [[UILabel alloc] initWithFrame:CGRectMake(50, 450, 150, 50)];
    _cacheLab.backgroundColor = [UIColor redColor];
    _cacheLab.text = _cacheString;
    NSLog(@"----------------%@",_cacheLab.text);
    _cacheLab.textColor = [UIColor blackColor];
    [self.view addSubview:_cacheLab];
    
    
}
-(void)viewDidAppear:(BOOL)animated{
    [self getFilePath];
    [self personalCenterInitUI];
}
// 显示有多少缓存的方法
-(NSString *)getFilePath{
    
    //文件管理
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    //缓存路径
    
    NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES);
    
    NSString *cacheDir = [cachePaths objectAtIndex:0];
    
    NSArray *cacheFileList;
    
    NSEnumerator *cacheEnumerator;
    
    NSString *cacheFilePath;
    
    unsigned long long cacheFolderSize = 0;
    
    cacheFileList = [fileManager subpathsOfDirectoryAtPath:cacheDir error:nil];
    
    cacheEnumerator = [cacheFileList objectEnumerator];
    
    while (cacheFilePath = [cacheEnumerator nextObject]) {
        
        NSDictionary *cacheFileAttributes = [fileManager attributesOfItemAtPath:[cacheDir stringByAppendingPathComponent:cacheFilePath] error:nil];
        
        cacheFolderSize += [cacheFileAttributes fileSize];
        
    }
    
    NSString * originalString  =[NSString stringWithFormat:@"%f",cacheFolderSize/1024.0/1024.0];
    
    _cacheString  =[NSString stringWithFormat:@"%@M",[originalString substringToIndex:4]];
    NSLog(@"---------------现在有缓存----------------%@",_cacheString);
    //单位MB
    return _cacheString;
    
}
#pragma UIButtonClick
// 按钮的点击事件
-(void)mainButtonClick:(UIButton *)sender{
    switch (sender.tag) {
        case 111:{//我的收藏
            CollectViewController *collect = [[CollectViewController alloc] init];
            [self.navigationController pushViewController:collect animated:YES];
        }
            
            break;
        case 112:{// 清除缓存
            
            dispatch_async(
                           dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                           , ^{
                               NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
                               NSLog(@"------清除缓存-------");
                               NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                               for (NSString *p in files) {
                                   NSError *error;
                                   NSString *path = [cachPath stringByAppendingPathComponent:p];
                                   if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                                       [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                                   }
                               }
                               
                           });
            _cacheLab.text = @"0";
            _finishAlert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您已成功清除缓存^_^" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
            [_finishAlert show];
        }
            
            break;
        case 113:{// 意见反馈
            
        }
            
            break;
        case 114:{// 版本更新
            
        }
            
            break;
            
        default:
            break;
    }
}

-(void)performDismiss:(NSTimer *)timer{
    [_finishAlert  dismissWithClickedButtonIndex:0 animated:NO];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
